/* This header file is shared by client & server. They really have
 * something to share...
 * */

/* Client/server protocol is as follows:
   Send INIT_PASSWD
   Send 64-bit cliserv_magic
   Send 64-bit size of exported device
   Send 128 bytes of zeros (reserved for future use)
 */

#ifndef __LICH_CLISERV_H__
#define __LICH_CLISERV_H__

#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>

#include "config.h"

#if 0
#if SIZEOF_UNSIGNED_SHORT_INT==4
typedef unsigned short uint32_t;
#elif SIZEOF_UNSIGNED_INT==4
typedef unsigned int uint32_t;
#elif SIZEOF_UNSIGNED_LONG_INT==4
typedef unsigned long uint32_t;
#else
typedef uint32_t uint32_t;
//#error I need at least some 32-bit type
#endif

#if SIZEOF_UNSIGNED_INT==8
typedef unsigned int uint64_t;
#elif SIZEOF_UNSIGNED_LONG_INT==8
typedef unsigned long uint64_t;
#elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
typedef unsigned long long uint64_t;
#else
typedef uint64_t uint64_t;
//#error I need at least some 64-bit type
#endif

#define uint32_t uint32_t
#define uint64_t uint64_t

#ifndef HAVE_FDATASYNC
#define fdatasync(arg) fsync(arg)
#endif

#if NBD_LFS==1
/* /usr/include/features.h (included from /usr/include/sys/types.h)
   defines this when _GNU_SOURCE is defined
 */
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#define _FILE_OFFSET_BITS 64
#endif

#ifndef G_GNUC_NORETURN
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#define G_GNUC_NORETURN __attribute__((__noreturn__))
#define G_GNUC_UNUSED __attribute__((unused))
#else
#define G_GNUC_NORETURN
#define G_GNUC_UNUSED
#endif
#endif

#define INFO(a) do { } while(0)

extern const uint64_t cliserv_magic;
extern const uint64_t opts_magic;
extern const uint64_t rep_magic;

#endif

#if 0
void setmysockopt(int sock);
void err_nonfatal(const char *s);

void err(const char *s) G_GNUC_NORETURN;

void logging(const char* name);

uint64_t ntohll(uint64_t a);
#endif

#define htonll ntohll

/* Options that the client can select to the server */
#define NBD_OPT_EXPORT_NAME	(1)	/** Client wants to select a named export (is followed by name of export) */
#define NBD_OPT_ABORT		(2)	/** Client wishes to abort negotiation */
#define NBD_OPT_LIST		(3)	/** Client request list of supported exports (not followed by data) */

/* Replies the server can send during negotiation */
#define NBD_REP_ACK		(1)	/** ACK a request. Data: option number to be acked */
#define NBD_REP_SERVER		(2)	/** Reply to NBD_OPT_LIST (one of these per server; must be followed by NBD_REP_ACK to signal the end of the list */
#define NBD_REP_FLAG_ERROR	(1 << 31)	/** If the high bit is set, the reply is an error */
#define NBD_REP_ERR_UNSUP	(1 | NBD_REP_FLAG_ERROR)	/** Client requested an option not understood by this version of the server */
#define NBD_REP_ERR_POLICY	(2 | NBD_REP_FLAG_ERROR)	/** Client requested an option not allowed by server configuration. (e.g., the option was disabled) */
#define NBD_REP_ERR_INVALID	(3 | NBD_REP_FLAG_ERROR)	/** Client issued an invalid request */
#define NBD_REP_ERR_PLATFORM	(4 | NBD_REP_FLAG_ERROR)	/** Option not supported on this platform */

/* Global flags */
#define NBD_FLAG_FIXED_NEWSTYLE (1 << 0)	/* new-style export that actually supports extending */
#define NBD_FLAG_NO_ZEROES	(1 << 1)	/* we won't send the 128 bits of zeroes if the client sends NBD_FLAG_C_NO_ZEROES */
/* Flags from client to server. Only one such option currently. */
#define NBD_FLAG_C_FIXED_NEWSTYLE NBD_FLAG_FIXED_NEWSTYLE
#define NBD_FLAG_C_NO_ZEROES	NBD_FLAG_NO_ZEROES

#endif
